function updateTime() {
    let now = new Date();
    let hours = now.getHours();
    let minutes = now.getMinutes();
    let seconds = now.getSeconds();
    minutes = minutes < 10 ? '0' + minutes : minutes;
    seconds = seconds < 10 ? '0' + seconds : seconds;
    let timeString = hours + ':' + minutes + ':' + seconds;
    document.getElementById('time-display').textContent = timeString;
}

setInterval(updateTime, 1000); // Update the time every second
updateTime(); // Initialize the time display

function showChineseDate() {
    let now = new Date();
    let year = now.getFullYear();
    let month = now.getMonth() + 1;
    let date = now.getDate();
    let weekdays = ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"];
    window.weekday = weekdays[now.getDay()];

    let chineseDate = year + "年" + (month < 10 ? "0" : "") + month + "月" + (date < 10 ? "0" : "") + date + "日 " + window.weekday;

    document.getElementById("chinese-date").textContent = `${chineseDate}  ${lunisolar().char8.year.stem.name + lunisolar().char8.year.branch.name}年 ${lunisolar().lunar.getMonthName()}${lunisolar().lunar.getDayName()}`;
}
showChineseDate();

function sentence() {
    // 定义API的URL
    const apiUrl = 'https://v1.hitokoto.cn';

    // 使用fetch发起GET请求
    fetch(apiUrl)
        .then(response => {
            if (response.ok) {
                return response.json(); // 将响应数据转换为JSON
            }
            throw new Error('Network response was not ok.');
        })
        .then(data => {
            console.log('API Response:', data); // 处理JSON数据
            let dom = document.getElementById("sentence")
            let text = data['hitokoto'] + '      ——' + data['from']
            if (dom.innerText) {
                // 文字删除效果
                deleteTextTransition(dom, () => {
                    console.log(text);

                    showTextTransition(dom, text)
                })
            } else {
                showTextTransition(dom, text)
            }

        })
        .catch(error => {
            console.error('API Request error:', error); // 处理错误
        });
}

sentence();
setInterval(sentence, 120000)



const bgList = [{
    url: './static/1.jpg',
    color: 'rgb(194 201 250)'
},
{
    url: './static/2.jpg',
    color: 'rgb(125 135 255);'
},
{
    url: './static/3.jpg',
    color: '#fff'
}
];

document.body.style = "background:url('./static/1.jpg') no-repeat center center fixed;background-size:cover;"

let index = 0;
setInterval(() => {
    index++;
    if (index > bgList.length - 1) {
        index = 0;
    }
    document.body.style = "background:url('" + bgList[index].url + "') no-repeat center center fixed;background-size:cover;"
    document.getElementById('root_color').innerHTML = `
    :root {
      --root-color: ${bgList[index].color};
    }
    `
}, 30000);


function deleteTextTransition(dom, cb) {
    let text = dom.innerText
    let i = text.length - 1
    let tim = setInterval(() => {
        del(dom, i)
        console.log(i);

        if (i < 0) {
            clearInterval(tim)
            cb && cb()
        }
        i--
    }, 70)
    function del(dom, i) {
        let t = dom.innerText
        let text = t.substring(0, i)
        dom.innerText = text
    }
}


function showTextTransition(dom, text, cb) {
    let i = 0;

    let tim = setInterval(() => {
        add(dom, text, i)
        console.log(i);

        if (i > text.length) {
            clearInterval(tim);
            cb && cb()
        }
        i++
    }, 70);

    function add(dom, text, i) {
        dom.innerText = text.substring(0, i)
        i++
    }
}

function addLesson () {
    let lessons = document.getElementById('class-sheet')
    for (let i=0; i<14; i++) {
        let lesson = document.createElement('div')
        if (i != 14) {
            lesson.setAttribute('id', 'lesson')
        }
        let sheet = {
            "星期一":['语','英','数','体','化','数','语','数','地','语','物','历','生','语'],
            "星期二":['英','数','语','历','英','化','地','生','政','数','化','数','英','物'],
            "星期三":['语','英','数','化','语','政','政','英','语','英','生','物','历','地'],
            "星期四":['英','数','历','英','物','英','体','语','数','物','政','化','英','物'],
            "星期五":['语','物','历','英','数','数','化','语','地','英','语','政','生','数'],
            "星期六":['英','语','历','数','物','语','语','地','英','化','生','数','化',' '],
            "星期五":['语','物','历','英','数','数','化','语','地','英','语','政','生','数'],
            "星期日":['历','英','语','物','数','物','数','历','历','活','活','历','政','班'],
        }
        lesson.innerText = sheet[window.weekday][i]
        lessons.appendChild(lesson)
    }
}

addLesson()